package com.letv.android.client.task;

import android.app.Activity;
import android.content.Context;

import com.letv.android.young.client.R;
import com.letv.android.client.utils.AlbumLaunchUtils;
import com.letv.android.client.view.LoadingDialog;
import com.letv.core.api.LetvRequest;
import com.letv.core.api.MediaAssetApi;
import com.letv.core.bean.DataHull;
import com.letv.core.bean.TopicDetailInfoListBean;
import com.letv.core.constant.PlayConstant;
import com.letv.core.network.volley.VolleyRequest;
import com.letv.core.network.volley.VolleyRequest.RequestManner;
import com.letv.core.network.volley.VolleyResponse.CacheResponseState;
import com.letv.core.network.volley.VolleyResponse.NetworkResponseState;
import com.letv.core.network.volley.toolbox.SimpleResponse;
import com.letv.core.network.volley.toolbox.VolleyDiskCache;
import com.letv.core.parser.TopicInfoListParser;
import com.letv.core.utils.LogInfo;
import com.letv.datastatistics.DataStatistics;
import com.letv.datastatistics.util.LetvErrorCode;
import com.ledim.utils.ToastUtil;

/**
 * 请求专题数据---针对 专辑 专题
 * 
 * @author hzz
 */
public class RequestTopicPlayTask {

    private Context mContext;
    // 专题ID
    private String mZid;
    // 状态提示Dialog
    private LoadingDialog loadingDialog = null;

    public RequestTopicPlayTask(Context context, String zid) {
        mContext = context;
        mZid = zid;
    }

    public void showDialog() {
        if (loadingDialog == null || !loadingDialog.isShowing()) {
            if (mContext instanceof Activity) {
                if (!((Activity) mContext).isFinishing() && !mContext.isRestricted()) {
                    try {
                        loadingDialog = new LoadingDialog(mContext, R.string.dialog_loading);
                        loadingDialog.setCancelable(true);
                        loadingDialog.show();
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void cancelDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            try {
                loadingDialog.dismiss();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void requestTopicPlayTask() {

        String markId;

        final boolean isFromTopicPage = false;

        final String dfssdf = MediaAssetApi.getInstance().getTopicDeatil(mContext,mZid, null);
        new LetvRequest<TopicDetailInfoListBean>(mContext, TopicDetailInfoListBean.class)
                .setUrl(MediaAssetApi.getInstance().getTopicDeatil(mContext,mZid, null))
                .setRequestType(RequestManner.CACHE_THEN_NETROWK).setParser(new TopicInfoListParser())
                .setCache(new VolleyDiskCache(mContext, "MyrequestTopicPlayTask"))
                .setCallback(new SimpleResponse<TopicDetailInfoListBean>() {

                    @Override
                    public void onNetworkResponse(VolleyRequest<TopicDetailInfoListBean> request,
                            TopicDetailInfoListBean result, DataHull hull, NetworkResponseState state) {
                        showDialog();
                        int type;
                        boolean isLocalSucceed = false;
                        boolean isZidError = false;

                        if (state == NetworkResponseState.SUCCESS) {
                            LogInfo.log("jzz", "1234s = " + dfssdf);
                            cancelDialog();
                            if (result != null && result.getList().size() > 0) {
                                type = result.getList().get(0).getType();
                                if (!isZidError && !isLocalSucceed) {
                                    if (isFromTopicPage) {
                                        AlbumLaunchUtils.launchTopic(mContext, Long.parseLong(mZid), result
                                                .getList().get(0).getType(), PlayConstant.LAUNCH_FROM_TOPIC_PAGE);
                                        // hzz
                                        // isFromTopicPage = false;
                                        // end
                                    } else {
                                        AlbumLaunchUtils.launchTopic(mContext, Long.parseLong(mZid), result
                                                .getList().get(0).getType(), PlayConstant.LAUNCH_FROM_CHANNEL);
                                    }
                                } else {

                                    ToastUtil.toast(
                                            R.string.topic_player_info);

                                }
                            } else {

                                ToastUtil.toast(
                                        R.string.topic_player_info);

                            }
                        } else if (state == NetworkResponseState.NETWORK_NOT_AVAILABLE) {
                            cancelDialog();
                            ToastUtil.toast(
                                    R.string.net_error);

                        } else if (state == NetworkResponseState.RESULT_ERROR) {
                            cancelDialog();
                            ToastUtil.toast(R.string.net_error);

                        } else if (state == NetworkResponseState.RESULT_NOT_UPDATE) {
                            cancelDialog();
                            ToastUtil.toast(
                                    R.string.topic_player_info);

                        } else if (state == NetworkResponseState.NETWORK_ERROR) {
                            cancelDialog();
                            ToastUtil.toast(R.string.net_no);

                        }
                    }

                    @Override
                    public void onCacheResponse(VolleyRequest<TopicDetailInfoListBean> request,
                            TopicDetailInfoListBean result, DataHull hull, CacheResponseState state) {
                        int type;
                        boolean isLocalSucceed = false;
                        boolean isZidError = false;
                        if (state == CacheResponseState.SUCCESS) {
                            if (result != null && result.getList().size() > 0) {
                                cancelDialog();
                                isLocalSucceed = true;
                                type = result.getList().get(0).getType();
                                if (!isZidError) {
                                    if (isFromTopicPage) {
                                        AlbumLaunchUtils.launchTopic(mContext, Long.parseLong(mZid), type,
                                                PlayConstant.LAUNCH_FROM_TOPIC_PAGE);
                                        // isFromTopicPage = false;
                                    } else {
                                        AlbumLaunchUtils.launchTopic(mContext, Long.parseLong(mZid), type,
                                                PlayConstant.LAUNCH_FROM_CHANNEL);
                                    }

                                }
                            }
                        }
                    }

                    @Override
                    public void onErrorReport(VolleyRequest<TopicDetailInfoListBean> request, String errorInfo) {
                        super.onErrorReport(request, errorInfo);
                        DataStatistics.getInstance().sendErrorInfo(mContext, "0", "0",
                                LetvErrorCode.LTURLModule_Subject_Detail, null, errorInfo, null, null, null, null);
                    }

                }).add();
    }
}
